package com.trainee.person.action;

import java.sql.SQLException;
import java.util.Scanner;

import com.trainee.person.dao.PersonDao;
import com.trainee.person.entity.Person;

public class PersonAction {

	public static void main(String[] args) {

		int choice;
		Scanner sc;

		System.out.println("Database connection established");
		System.out.println("1St : To View All The Records");
		System.out.println("2nd : To Add a record into database.");
		System.out.println("3rd : To Update a record into database.");
		System.out.println("4th : To Delete a record from database.");
		System.out.println("5th : To Exit");
		
		try {
			do {
				sc = new Scanner(System.in);
				choice = Integer.parseInt(sc.nextLine());

				switch (choice) {
				case 1:
					System.out.println("My First Choice perfomed.");
					Person person = new Person();
					PersonDao perrsonDao = new PersonDao();
					boolean flag = perrsonDao.getPersonInfo(person);
					if (flag) {
						System.out
								.println("Finally Informations are successfully inserted !");
					} else {
						System.out.println("Error while insert person details");
					}

					break;

				case 2:
					System.out.println("How many rocords you wants to insert?");
					int len = Integer.parseInt(sc.nextLine());

					for (int i = 0; i < len; i++) {
						System.out.println("Record" + " " + (i + 1)
								+ "Enter your name? ");
						String name = sc.nextLine();
						System.out.println("Record" + " " + (i + 1)
								+ "Enter your Designation? ");
						String designation = sc.nextLine();
						System.out.println("Record" + " " + (i + 1)
								+ "Enter your company-name? ");
						String company = sc.nextLine();

						Person personEntity = new Person(name, designation,
								company);
						PersonDao personDao = new PersonDao();
						boolean flag1;

						flag1 = personDao.insertPersonInfo(personEntity);

						if (flag1) {
							System.out
									.println("Finally Informations are successfully inserted !");
						} else {
							System.out
									.println("Error while insert person details");
						}

					}
					break;

				case 3:

					System.out
							.println(" If you want to Update the records then Enter your name.?");
					String name = sc.nextLine();

					System.out.println("Enter your Designation?");
					String design = sc.nextLine();

					System.out.println("Enter your new company-name?");
					String new_company = sc.nextLine();

					Person personEntity = new Person(name, design, new_company);
					PersonDao personDao = new PersonDao();
					personDao.UpdatePersonInfo(personEntity);

					break;

				case 4:
					break;

				case 5:
					break;

				default:
					System.out.println("Invalid Choice");
					break;
				}

			} while (choice != 5);
		} catch (SQLException e) {
			System.err.print(e.getMessage());
		}
	}
}
